#!/bin/bash
set -e

repoName=${IMAGE_NAME%%:*}
oldTag=${IMAGE_NAME#*:}

major=${oldTag%%.*}
minor=${oldTag#*.}
minor=${minor%%.*}
bugfix=${oldTag#*.}
bugfix=${bugfix#*.}
bugfix=${bugfix#*.}
bugfix=${bugfix%%-*}

newTag=$major.$minor
newTagBugfix=$major.$minor.$bugfix

for tag in {$newTag,$newTagBugfix,latest}; do
    docker tag $IMAGE_NAME ${repoName}:${tag}
    docker push ${repoName}:${tag}
done